package models

import (
	"t11Lib/t11Errors"
	"t11Lib/t11Model"
	"t11Lib/t11Util"
)

type DeviceLog struct {
	t11Model.BaseMdl
	Info  string
	DevId string
}

type deviceLogDao struct{}

var DeviceLogDao deviceLogDao

func (self deviceLogDao) Create(conn *t11Model.DbConn, devId, info string) t11Errors.T11Error {
	devLog := &DeviceLog{}
	devLog.Id = t11Util.RandMd5()
	devLog.Info = info
	devLog.DevId = devId
	_, e := conn.Insert(devLog)
	return t11Errors.Wrap(e)
}

func (self deviceLogDao) GetByDevIds(conn *t11Model.DbConn, devIds []string) ([]*DeviceLog, t11Errors.T11Error) {
	if len(devIds) == 0 {
		return nil, nil
	}
	var ret []*DeviceLog
	_, err := conn.QueryTable(&DeviceLog{}).Filter("DevId__in", devIds).All(&ret)
	return ret, t11Errors.Wrap(err)
}

func (self deviceLogDao) DeleteByDevId(conn *t11Model.DbConn, devIds []string) t11Errors.T11Error {
	if len(devIds) == 0 {
		return nil
	}
	_, e := conn.QueryTable(&DeviceLog{}).Filter("DevId__in", devIds).Delete()
	return t11Errors.Wrap(e)
}
